package com.androidgame.framework.impl;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.Window;
import android.view.WindowManager;

import com.androidgame.framework.Audio;
import com.androidgame.framework.Game;
import com.androidgame.framework.Graphics;
import com.androidgame.framework.Screen;

//AndroidGame extend the Activity class and
//implements the Game interface
//its the enrty point into the app
public abstract class AndroidGame extends Activity implements Game 
{
	//will manage our main loop thread for us
	AndroidFastRenderView renderView;
	
	//will be set to instances of AndroidGraphics, AndroidAudio, AndroidInput, and AndroidFileIO
	Graphics graphics;
	Audio audio;
	//Input input;
	//FileIO fileIO;
	
	//holds the currently active Screen
	Screen screen;
	
	//use to keep the screen from dimming
	WakeLock wakeLock;
	
        //first function to be called
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		//make the Activity full-screen
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		//set up our artificial framebuffer depending on the orientation
		//determine the Activity�s screen orientations from a class called Configuration
		boolean isLandscape = getResources().getConfiguration().orientation ==
				Configuration.ORIENTATION_LANDSCAPE;

		int frameBufferWidth = isLandscape ? 480 : 320;

		int frameBufferHeight = isLandscape ? 320 : 480;

		//set the framebuffer size and instantiate a Bitmap
		Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
				frameBufferHeight, Config.RGB_565);

		//calculate the scaleX and scaleY values that the SingleTouchHandler and the
		//MultiTouchHandler classes will use to transform the touch event coordinates in our
		//fixed-coordinate system
		float scaleX = (float) frameBufferWidth
				/ getWindowManager().getDefaultDisplay().getWidth();

		float scaleY = (float) frameBufferHeight
				/ getWindowManager().getDefaultDisplay().getHeight();

		//set to instances of AndroidGraphics, AndroidAudio, AndroidInput, and AndroidFileIO
		renderView = new AndroidFastRenderView(this, frameBuffer);

		graphics = new AndroidGraphics(getAssets(), frameBuffer);

		//fileIO = new AndroidFileIO(getAssets());

		audio = new AndroidAudio(this);

		//input = new AndroidInput(this, renderView, scaleX, scaleY);

		//holds the currently active Screen
		
		//our game will implement, and set the AndroidFastRenderView 
		//as the content view of the Activity.
		screen = getStartScreen();

		setContentView(renderView);

		PowerManager powerManager = (PowerManager)
				getSystemService(Context.POWER_SERVICE);

		//WakeLock to keep the screen from dimming
		wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
	}

        //second function to be called
	public void onResume() 
	{
		super.onResume();
		wakeLock.acquire();
		screen.resume();
		renderView.resume();
	}

	public void onPause() 
	{
		super.onPause();
		wakeLock.release();
		renderView.pause();
		screen.pause();

		if (isFinishing())
		{
			screen.dispose();
		}
	}

//	public Input getInput() 
//	{
//		return input;
//	}
//
//	public FileIO getFileIO() 
//	{
//		return fileIO;
//	}
//
	public Graphics getGraphics() 
	{
		return graphics;
	}

	public Audio getAudio() 
	{
		return audio;
	}
	//	The getInput(), getFileIO(), getGraphics(), and getAudio() methods should need no
	//	explanation. We simply return the respective instances to the caller. The caller will
	//	always be one of our Screen implementations of our game later on.

	public void setScreen(Screen screen) 
	{
		if (screen == null)
		{
			throw new IllegalArgumentException("Screen must not be null");
		}

		this.screen.pause();

		this.screen.dispose();

		screen.resume();

		screen.update(0);

		this.screen = screen;
	}

	public Screen getCurrentScreen()
	{
		return screen;
	}
}
